const { ObjectId } = require('mongodb')
const UserModel = require('../../model')

// 发送通知
exports.addNotice = (req, res) => {
  const data = req.body
  const newNotice = UserModel.FriendSchema({
    ...data
  })
  UserModel.FriendSchema.find({ u_id: data.u_id, friend_id: data.friend_id }, (err, result) => {
    if (data.u_id == data.friend_id) return res.cc('不能添加自己')
    UserModel.FriendListSchema.find({
      $or: [{
        $and: [{ u_id: data.u_id }, { friend_id: data.friend_id }],
      }, {
        $and: [{ u_id: data.friend_id }, { friend_id: data.u_id }]
      }]
    }, (error, results) => {
      if (error) return res.cc(error)
      if (results.length) {
        return res.cc({ status: 400, message: '你们已经是好友了' })
      } else {
        if (err) return res.cc(err)
        if (result.length) {
          return res.send({ status: 200, message: '发送成功，等待对方同意' })
        }
        newNotice.save((err, result) => {
          if (err) return res.cc(err)
          return res.send({ status: 200, message: '发送成功，等待对方同意' })
        })
      }
    })
  })
}
// 获取通知
exports.getNotice = (req, res) => {
  const id = req.user._id
  UserModel.FriendSchema.find({ friend_id: id }, (err, result) => {
    if (err) return res.cc(err)
    const data = result.map((item) => {
      return {
        noticeId: item._id,
        id: item.friend_id,
        friend_id: item.u_id,
        friend_info: JSON.parse(item.userInfo)
      }
    })
    res.send({
      status: 200,
      data
    })
  })
}

// exports.dealNotice = (req, res) => {
//   const data = req.body
//   UserModel.FriendSchema.findOneAndDelete({
//     u_id: data.u_id, friend_id: data.friend_id
//   })
// }
// 同意好友申请
exports.AgreeNotice = (req, res) => {
  const data = req.body
  UserModel.FriendSchema.findByIdAndDelete({ _id: data.noticeId }, (err, result) => {
    UserModel.FriendListSchema.find({
      $or: [{
        $and: [{ u_id: result.u_id, }, { friend_id: result.friend_id }]
      },
      {
        $and: [{ u_id: result.friend_id }, { friend_id: result.u_id }]
      }]
    }, (error, results) => {
      if (error) return res.cc(error)
      if (results.length) {
        return res.send({ status: 400, message: '已经是好友了' })
      } else {
        if (result) {
          UserModel.FriendListSchema({
            u_id: result.friend_id,
            friend_id: result.u_id,
            friend_info: JSON.parse(result.userInfo),
            u_info: JSON.parse(data.u_info),
          }).save((error, results) => {
            if (error) return res.cc(error)
            res.send({ status: 200, message: '成功添加好友' })
          })
        } else {
          res.send({ status: 400, message: '错误notice' })
        }
      }
    })
  })
}

// 拒绝好友申请
exports.RejectNotice = (req, res) => {
  const data = req.body
  UserModel.FriendSchema.findByIdAndDelete({ _id: data.noticeId }, (err, result) => {
    if (err) return res.cc(err)
    if (result) {
      res.send({ status: 200, message: '拒绝' })
    } else {
      res.send({ status: 400, messgae: '错误notice' })
    }
  })
}

// 获取好友列表
exports.getFriend = (req, res) => {
  const id = req.user._id
  console.log(id)
  UserModel.FriendListSchema.find({ $or: [{ u_id: id }, { friend_id: id }] }, (err, result) => {
    if (err) return res.cc(err)
    res.send({ status: 200, result })
  })
}